﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RBOSS.Business.CustomerModule;

namespace Online_Shopping_System
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        public event EventHandler CheckoutClick;
        public void SetTotalPrice(decimal totalPrice)
        {
            lblTotalPrice.Text = totalPrice.ToString();
        }

        public void SetLoginText(string text)
        {
            butLogout.Text = text;
        }

        public void SetAmountInCart(int amount)
        {
            lblTotalItems.Text = amount.ToString();
        }

        public void ShowCheckoutButton(bool value)
        {
            butCheckout.Visible = value;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            WireupEvent();
            if (Session["email"] != null)
            {
                SetLoginText("Log out");
                lblUserName.Text = Session["name"].ToString();
            }
            else
            {
                lblUserName.Text = "Welcome Guest";
                SetLoginText("Log in");
            }
        }

        private void WireupEvent()
        {
            butCheckout.Click += (sender, e) => CheckoutClick(sender, e);
            butLogout.Click += LogoutClick;
            butSearch.Click += SearchClick;
        }

        private void SearchClick(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?s=" +txbSearch.Text.Trim());
        }

        private void LogoutClick(object sender, EventArgs e)
        {
            butLogout.Text = "Log in";
            Session["email"] = null;
            Session["role"] = null;
            var customer = CustomerSingleton.GetCustomer();
            if (customer.IsLogin)
            {
                CustomerSingleton.ClearSession();
            }
            Response.Redirect("Login.aspx");
        }
    }
}